﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Intelligencia.UrlRewriter.Transforms;

public partial class Default : System.Web.UI.Page
{
    protected string sMainArticleTitle = string.Empty;
    protected string sMainArticleLink = string.Empty;
    protected string sMainArticleSubTitle = string.Empty;
    protected string sMainArticleImage = string.Empty;
    protected string sHPStaticTitle = string.Empty;
    protected string sHPStaticContent = string.Empty;

    string sImagesPath = "picture_library/";

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Name = "keywords";
        meta.Content = PageHeaderData.GetPageKeywordsByType(NewsType.Default);
        this.Header.Controls.Add(meta);
        meta = new HtmlMeta();
        //Descrioption
        CategoryItem catItem=Category.ItemById((int)NewsType.Default);
        meta.Name = "description";
        meta.Content = catItem.cDescription;
        this.Header.Controls.Add(meta);

        this.Title = catItem.cTitle;

        DataSet DS=Dal.ExecuteDBspDS("usp_GetMainPage");
        if(DS!=null)
            if (DS.Tables.Count > 0)
            {
                if (DS.Tables[0].Rows.Count > 0)
                {
                    sMainArticleTitle = DS.Tables[0].Rows[0]["Title"].ToString();
                    sMainArticleSubTitle = DS.Tables[0].Rows[0]["subTitle"].ToString();
                    sMainArticleLink = DS.Tables[0].Rows[0]["cPageName"].ToString();
                    sMainArticleLink = HttpUtility.UrlEncode(sMainArticleLink);
                    sMainArticleImage = string.Format("{0}{1}", sImagesPath, (DS.Tables[0].Rows[0]["img439x217"].ToString().Length > 0) ? DS.Tables[0].Rows[0]["img439x217"].ToString() : "439x217.jpg");

                    DataTable DTthreeNews = new DataTable();

                    DTthreeNews = DS.Tables[0].Clone();
                    for (int iCount = 1; iCount < DS.Tables[0].Rows.Count; iCount++)
                    {
                        DTthreeNews.NewRow();
                        DataRow dr = DTthreeNews.NewRow();
                        dr.ItemArray = DS.Tables[0].Rows[iCount].ItemArray;
                        DTthreeNews.Rows.Add(dr);
                    }

                    rptNews.DataSource = DTthreeNews;
                    rptNews.DataBind();
                }
                dtlPopular.DataSource = DS.Tables[1];
                dtlPopular.DataBind();

                if(DS.Tables.Count>2)
                    if (DS.Tables[2].Rows.Count > 0)
                    {
                        sHPStaticTitle=DS.Tables[2].Rows[0]["Title"].ToString();
                        sHPStaticContent = DS.Tables[2].Rows[0]["Content"].ToString();
                    }
            }
            rptReferences.DataSource = Dal.ExecuteDBsp("usp_GetTopReferences");
            rptReferences.DataBind();
        
        InitEncyclopedy();
    }

    private void InitEncyclopedy()
    {
        List<abItem> abArray = ab.GetAbArrayList();
        List<CategoryItem> CatList = Category.ByAncestors((int)NewsType.Wiki);
        if (CatList != null)
        {
            // Set Enciclopedy
            StringBuilder strLinks = new StringBuilder();

            if (abArray != null)
                for (int iCount = 0; iCount < 27; iCount++)
                {
                    abItem abitem = abArray[iCount];
                    string sText = string.Format("<li><a href=\"#tab{0}\"><span>{1}</span></a></li>", abitem.abValue, abitem.abText.ToUpper());
                    UL.InnerHtml += sText;
                    //strCategorys.
                }
            List<CategoryItem> filteredList;
            for (int iCount = 0; iCount < 27; iCount++)
            {
                abItem abItem = abArray[iCount];
                strLinks.Append(@"<div class=""wiki1""  id=""tab" + abItem.abValue + @""">");
                //a href to wikiA
                filteredList = CatList.FindAll(new HandFinderKeysbyId(abItem.abText).Match);
                if (filteredList != null)
                    foreach (CategoryItem catItem in filteredList)
                    {

                        string sText = string.Format("<a href=\"{0}-w.aspx\">{1}</a>", HttpContext.Current.Server.UrlEncode(catItem.cPageName), HttpContext.Current.Server.HtmlEncode(catItem.cTitle));
                        strLinks.Append(sText);

                    }
                strLinks.Append("</div>");
            }
            divWiki.InnerHtml += strLinks.ToString();
        }
    }
}
